package com.aaa.service.sas.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.dao.sas.TestPlanSasDao;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.sas.TestPlanSas;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.service.sas.TestPlanSasServices;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @author ：马明钢
 * @date ：Created in 2022/8/30 19:28
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TestPlanSasServicesImpl implements TestPlanSasServices {
    @Autowired
    TestPlanSasDao testPlanSasDao;

    /**
     *  * @create by: 马明钢 
     *
     * @param page 
     * @return com.github.pagehelper.Page 
     * @description: 分页排序查询 
     * @create time: 2022/9/2 19:32 * @param ts
     */
    @Override
    public Page queryAll(TestPlanSas ts, PageSortEntity page) {
        PageHelper.startPage(page.getPage(), page.getLimit(), page.getOrderString());
        List<TestPlanSas> testPlanSas = testPlanSasDao.queryAll(ts);
        Page page1 = (Page) testPlanSas;
        return page1;
    }

    /**
     *  * @create by: 马明钢 
     *
     * @return boolean 
     * @description:  保存
     * @create time: 2022/9/2 19:33 * @param ts 
     */
    @InsertAnn
    @Override
    public boolean insert(TestPlanSas testPlanSas) {
        return testPlanSasDao.insert(testPlanSas);
    }


    /**
     *  * @create by: 马明钢 
     *
     * @return boolean 
     * @description:  根据id删除
     * @create time: 2022/9/2 19:34 * @param id 
     */
    @Override
    public boolean delete(String id) {
        return testPlanSasDao.delete(id);
    }

    /**
     *  * @create by: 马明钢 
     *
     * @return boolean 
     * @description:  修改
     * @create time: 2022/9/2 19:34 * @param ts 
     */
    @UpdateAnn
    @Override
    public boolean update(TestPlanSas ts) {
        return testPlanSasDao.update(ts);
    }

    /**
     *  * @create by: 马明钢 
     *
     * @return long 
     * @description:  生成并查询邀请是否重复
     * @create time: 2022/9/2 19:35 * @param  
     */
    @Override
    public Integer queryByTestCode() {
        Integer random = 0;
        random = this.random();
        while (testPlanSasDao.queryByTestCode(random).size() > 0) {
            random = this.random();
        }
        return random;
    }

    /**
     *  * @create by: 马明钢 
     *
     * @return boolean 
     * @description:  添加和修改题目是否重复
     * @create time: 2022/9/2 19:36 * @param ts 
     */
    @Override
    public boolean repetitionName(TestPlanSas ts) {
        TestResultSas tss = testPlanSasDao.repetitionName(ts);
        if (ObjectUtil.isEmpty(tss) || ts.getId().equals(tss.getId())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *  * @create by: 马明钢 
     *
     * @return long 
     * @description:  生成随机邀请码
     * @create time: 2022/9/2 19:37 * @param  
     */
    public Integer random() {
        Integer flag = 0;
        for (int i = 0; i <= 100; i++) {
            flag = new Random().nextInt(999999);
            if (flag < 100000) {
                flag += 100000;
            }

        }
        return flag;
    }
}
